package com.test.springboot3.springboot3testdemo1.controller;

import io.micrometer.common.util.StringUtils;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created with IntelliJ IDEA.
 *
 * @author： liuziyang
 * @date： 2024/8/2-下午3:35
 * @description：
 * @modifiedBy：
 * @version: 1.0
 */
@Slf4j
@RequiredArgsConstructor
@Controller
public class IndexController {
  private final HttpSession httpSession;

  @ResponseBody
  @RequestMapping(value = "/login")
  public String login() {
    return "login page";
  }

  @RequestMapping("/login/submit")
  public String loginSubmit(@RequestParam("username") String userName) {
    if (StringUtils.isNotBlank(userName)) {
      httpSession.setAttribute("username", userName);
      return "/index";
    }
    return "/login";
  }

  @ResponseBody
  @RequestMapping("/index")
  public String index() {
    log.info("session id :{}", httpSession.getId());
    return "index page";
  }

  @RequestMapping("/logout")
  public String logout() {
    httpSession.invalidate();
    return "/login";
  }
}
